/*
 * @FilePath: App.java
 * @Author: lolan b1723552783@outlook.com
 * @Date: 2022-10-03 11:32:18
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-11-10 18:25:37
 * Copyright: 2022 xxxTech CO.,LTD. All Rights Reserved.
 * @Descripttion: 
 */
package com.lolan.training;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Set;

import okhttp3.*;


import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Hello world!
 *
 */
public class App 
{
    private static ObjectMapper MAPPER = new ObjectMapper();
    // public static String getHtmlByPost(String urlString,String postBody) throws IOException {
    //     OkHttpClient client = new OkHttpClient().newBuilder()
    //  .build();
    //     MediaType mediaType = MediaType.parse("application/json");
    //     RequestBody body = RequestBody.create(mediaType, "{\"jsonrpc\": \"2.0\",\"method\": \"call\",\"params\": {\r\n \"model\": \"ps.workflow.server\",\r\n \"method\": \"p_exec_services\",\r\n \"data\": {\r\n     \"bill_id\": 8132,\r\n     \"services\": [\r\n  {\r\n      \"function\": \"svc_std_pre_audit\",\r\n      \"model\": \"studio.base\"\r\n  }\r\n     ],\r\n     \"model_id\": \"iv.input.vat.inv9\"\r\n }\r\n    },\r\n    \"id\": null\r\n}");
    //     Request request = new Request.Builder()
    //  .url("http://192.168.50.16:4090/web/workflow/workflow_info?x=1")
    //  .method("POST", body)
    //  .addHeader("Cookie", "session_id=9c20271c7a9521866cb8f3a282ea2fd6a8323495; csrftoken=fgCq0MUAvhuneuKXpaA7QDfyh93dV0hx4A9Vk8keDI9wZaRqpb30bQI5vNr3FzSa;")
    //  .addHeader("Content-Type", "application/json")
    //  .build();
    //     Response response = client.newCall(request).execute();
    //     return "ok";
    // }

//     public static void main(String[] args) throws IOException {
//  String b = "{\"jsonrpc\": \"2.0\",\"method\": \"call\",\"params\": {\"model\": \"ps.workflow.server\",\"method\": \"p_exec_services\", " +
//   "\"data\": {\"bill_id\": 8132,\"services\": [{\"function\": \"svc_std_pre_audit\",\"model\": \"studio.base\"" +
//   "}],\"model_id\": \"iv.input.vat.inv9\"},\"id\": null}";
//  System.out.println(getHtmlByPost("http://192.168.50.16:4090/web/workflow/workflow_info",b));
// // String url="输入网址";
// // String postBody="输入body参数，postman -》body里面直接复制就行";
// // try{
// //     System.out.println(getHtmlByPost(url,postBody));
// // } catch (IOException e) {
// //     e.printStackTrace();
// // }
//  return;
//     }

    @SuppressWarnings("unchecked")
    public static void formFinalString(LinkedHashMap<String, Object> map, StringBuilder new_param) throws IOException{
        Set<String> set = map.keySet();
        Object[] strs = set.toArray();
        Arrays.sort(strs);
        for (int i = 0; i < strs.length; i++) {
            new_param.append(strs[i]);
            Object obj = map.get(strs[i]);
            if (obj == null){
                new_param.append("null");
                new_param.append("&");
                continue;
            }
            if (obj.getClass() == LinkedHashMap.class){
                formFinalString((LinkedHashMap<String, Object>)obj, new_param);
                new_param.append("&");
                continue;
            }
            if (obj.getClass() == ArrayList.class){
                new_param.append(MAPPER.writeValueAsString(obj).replace(" ", ""));
                new_param.append("&");
                continue;
            }
            new_param.append(MAPPER.writeValueAsString(obj));
            new_param.append("&");
        }
    }

    public static String getSign(String s) throws UnsupportedEncodingException, NoSuchAlgorithmException{
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(s.getBytes("UTF-8"));
        String sign = new BigInteger(1, md.digest()).toString(16);
        System.out.println(sign);
        return sign;
    }


    public static void main(String[] args) throws NoSuchAlgorithmException, IOException{
        String jsonString = "[{\"create_uid_number\": \"\", \"project_number\": \"\", \"stk_org_number\": \"1001\", \"customer_number\": \"010121\", \"dept_number\": \"0405\", \"biz_date\": \"2023-04-25T14:12:25.823+08:00\", \"line_ids\": [{\"stk_uom_name\": \"只\", \"stk_qty\": 1.0, \"warehouse_number\": \"03\", \"material_number\": \"030303001\", \"stk_state_name\": \"正常\", \"stk_org_number\": \"1001\"}]}]";
        String secretKey = "fuqMzVqWFmJQ5RsVE6zsfbEhtzv1ZjJt";
        boolean isMap = false;
        StringBuilder sb = new StringBuilder();
        if (isMap){
            // convert JSON string to Map
            LinkedHashMap<String, Object> map = MAPPER.readValue(jsonString, LinkedHashMap.class);
            formFinalString(map, sb);
            System.out.println(map);
        }else{
            ArrayList<LinkedHashMap> list = MAPPER.readValue(jsonString, ArrayList.class);
            for (LinkedHashMap map: list){
                formFinalString(map, sb);
            }
        }
        String sign = getSign(sb.toString() + secretKey);
        System.out.println(sb.toString());
        System.out.println(sign);
        return;
    }
}
